//
//  MainViewController.swift
//  Onexinlang
//
//  Created by Mac on 15/9/1.
//  Copyright © 2015年 HM. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

       view.backgroundColor = UIColor.whiteColor()
        
        addChildViewControllers()
        
        
    
        
    }
    
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
            setupComposedButton()
    }
    
    
    @objc private func clickComposedButton() {
        
        print(__FUNCTION__)
    }

    
    private func setupComposedButton() {
        
        
        let count = childViewControllers.count
        
        let w = tabBar.bounds.width / CGFloat(count)
        
        let rect = CGRect(x: 0, y: 0, width: w, height: tabBar.bounds.height)
        
        composedButton.frame = CGRectOffset(rect, 2 * w, 0)
    }
   
    
    
    
    
    
    private func addChildViewControllers() {
        
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        
        
        
    }
    
    
    
    
    private func addChildViewController(vc: UIViewController, title: String , imageName: String) {
    
         vc.title = title
    
        vc.tabBarItem.image = UIImage(named: imageName)
    
        let nav = UINavigationController(rootViewController: vc)
    
       addChildViewController(nav)
    
    
    }
    
    private lazy var composedButton:UIButton = {
        
        
        let btn = UIButton()
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted);
        
        self.tabBar.addSubview(btn)
        
        btn.addTarget(self, action: "clickComposedButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        
        return btn
        
        
    }()
    

}
